﻿using NLog;
using NLog.Config;
using NLog.Internal;
using NLog.LayoutRenderers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Sprout.NLog
{
    /// <summary>
    /// 项目名称.对应微服务的项目名称,比如 marisfrolg.order.api
    /// </summary>
    [LayoutRenderer("app_name")]
    [ThreadAgnostic]
    [ThreadSafe]
    public class AppNameLayoutRenderer : LayoutRenderer
    {
        private static string appName = "";
        /// <summary>
        /// 将主项目名作为app_name
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="logEvent"></param>
        protected override void Append(StringBuilder builder, LogEventInfo logEvent)
        {
            {
                try
                 {
                    appName = Assembly.GetEntryAssembly().GetName().Name;
                }
                catch(Exception ex)
                {
                    appName = "-";
                }
            }
            builder.Append(appName);
        }
    }
}
